#include "CUserDailyTask.h"
#include "../mainctrl.h"
#include "commmsg.h"

void CUserDailyTask::Clean()
{
    CT_TRACE(("清理 UserDailyTask"));
    
    _iLiveValuer = 0;
    _aOpenedTBox.Clear();

    for (int iIndex = this->_hTaskCounts.GetHeadIndex(); iIndex >= 0; iIndex = this->_hTaskCounts.GetHeadIndex())
    {
        this->_hTaskCounts.HashFreeByIndex(iIndex);
    }

    _aClassTypeAwardeds.Clear();
    m_bTaskTargetStatus = false;
    m_UpdateTime = 0;
}

size_t CUserDailyTask::CountSize()
{
    return CTLib::CTHashObjectMgr<int, CDailyTaskIdStatusData>::CountSize(MAX_DAILY_TASK_CONFIG);
}

int CUserDailyTask::GetYesterdayFinishId(int iTaskId)
{
    int iIndex = this->_hTaskCounts.HashFind(iTaskId);
    if( iIndex<0 )
    {
        return ERROR_DTASK_TASK_ID;
    }

    return this->_hTaskCounts[iIndex]._stYesterdayTaskFinish.Get( CMainCtrl::m_pstMain->m_iNow-86400 );
}

int CUserDailyTask::FindYesterdayTask(int iTaskId)
{
    int iRet = 0;

    int iIndex = this->_hTaskCounts.HashFind(iTaskId);
    if( iIndex<0 )
    {
        return ERROR_DTASK_TASK_ID;
    }

    CDailyTaskIdStatusData & cdt = this->_hTaskCounts[iIndex];
    cdt._stYesterdayTaskFinish.Add( 1, CMainCtrl::m_pstMain->m_iNow-86400 );
    cdt._iFindYesterdayOp = cdt._iCount;
    cdt._iCount = 0;

    m_bTaskTargetStatus = true;

    this->UpdateTaskStatus(false); //只保存，不推送到前端

    return iRet;
}

int CUserDailyTask::GetFinishCountsWithId(int iTaskId)
{
    int iIndex = this->_hTaskCounts.HashFind(iTaskId);
    if( iIndex<0 )
    {
        return ERROR_DTASK_TASK_ID;
    }

    return this->_hTaskCounts[iIndex]._iCount;
}

int CUserDailyTask::GetCurLiveness()
{
    return this->_iLiveValuer;
}

//获取小秘书等级
int CUserDailyTask::GetSecretaryLevel()
{
    return this->_stSecretary._iLevel;
}

//获取小秘书亲密度
int CUserDailyTask::GetIntimacyValue()
{
    return this->_stSecretary._iIntimacy;
}

//设置小秘书等级
void CUserDailyTask::SetIntimacyLevel(int iNewSecretaryLevel)
{
    this->_stSecretary._iLevel = iNewSecretaryLevel;

    m_bTaskTargetStatus = true;

    this->UpdateTaskStatus(false); //只保存，不推送到前端
}

int CUserDailyTask::OpenClassTBoxWithId(int iTBoxClassTypeId)
{
    this->_aClassTypeAwardeds[iTBoxClassTypeId] = CONST_TBOX_OPENED; 

    m_bTaskTargetStatus = true;

    this->UpdateTaskStatus(false); //只保存，不推送到前端

    return 0;
}

int CUserDailyTask::OpenTBoxWithId(unsigned int iTBoxId)
{
    if( this->_aOpenedTBox[iTBoxId-1]>=CONST_DOUBLE_AWARD_BASE )
    {
        this->_aOpenedTBox[iTBoxId-1] = CONST_DOUBLE_AWARD_BASE + CONST_TBOX_OPENED; 
    }
    else
    {
        this->_aOpenedTBox[iTBoxId-1] = CONST_TBOX_OPENED; 
    }

    //增长亲密度
    this->_stSecretary._iIntimacy += CONST_ADD_INTIMACY;

    m_bTaskTargetStatus = true;

    this->UpdateTaskStatus(false); //只保存，不推送到前端

    return 0;
}

int CUserDailyTask::GetClassTBoxStatusWithId(int iTBoxClassTypeId)
{
    return this->_aClassTypeAwardeds[iTBoxClassTypeId];
}

int CUserDailyTask::GetTBoxStatusWithId(unsigned int iTBoxId)
{
    return this->_aOpenedTBox[iTBoxId-1];
}

int CUserDailyTask::GetCurrentFinishCounts(unsigned int iTaskID)
{
    int iIndex = this->_hTaskCounts.HashFind(iTaskID);
    if( iIndex<0 )
    {
        return ERROR_DTASK_TASK_ID;
    }

    return this->_hTaskCounts[iIndex]._iCount;
}

int CUserDailyTask::TriggerTaskFinish(unsigned int iTaskID, int iCount)
{
    if (iCount <= 0)
    {
        return 0;
    }
    
    int iRet = 0;

    int iIndex = this->_hTaskCounts.HashFind(iTaskID);
    if( iIndex<0 )
    {
        return ERROR_DTASK_TASK_ID;
    }

    if( (unsigned int)this->_hTaskCounts[iIndex]._iTaskId != iTaskID )
    {
       return ERROR_DTASK_TASK_ID; 
    }

    this->_hTaskCounts[iIndex]._iCount += iCount;
    //增加5点活跃度
    this->_iLiveValuer += 5 * iCount;
    //触发类型完成奖励

    //根据活跃度更新宝箱的状态tbox 
    this->UpdateTBoxStatus(this->_iLiveValuer); 

    //检查状态时否更新
    m_bTaskTargetStatus = true;

    this->UpdateTaskStatus(false); //只保存，不推送到前端

    return iRet;
}

//根据活跃度更新宝箱的状态tbox 
void CUserDailyTask::UpdateTBoxStatus(int iNewLivess)
{
    int iTBoxId = 0;
    
    iTBoxId = CMainCtrl::m_pstMain->m_stDailyTaskMgr.GetMaxTBoxIdWIthLiveness(iNewLivess);

    for( int i=1; i<iTBoxId; ++i )
    {
        if( this->_aOpenedTBox[(i-1)%MAX_TBOX_NUM] == CONST_TBOX_CANOT_OPEN )
        {
            this->_aOpenedTBox[(i-1)%MAX_TBOX_NUM] = CONST_TBOX_CAN_OPEN;
        }
        else if( this->_aOpenedTBox[(i-1)%MAX_TBOX_NUM] == (CONST_DOUBLE_AWARD_BASE+CONST_TBOX_CANOT_OPEN) )
        {
            this->_aOpenedTBox[(i-1)%MAX_TBOX_NUM] = CONST_DOUBLE_AWARD_BASE+CONST_TBOX_CAN_OPEN;
        }
    }
}

//检查所有同一类型的任务是否完成
int CUserDailyTask::UpdateClassTypeFinish(int _iTaskBelong)
{
    int iRet = 0;
    int iFinishCounts = 0; //完成的次数
    int iAllBelongCounts = 0; //此类型所有的任务数
    unsigned int iDestNum = 0;
    bool bHasCeiling = false;
    unsigned int iTaskId = 0;

    for (int iIndex = this->_hTaskCounts.GetHeadIndex(); iIndex >= 0; iIndex = this->_hTaskCounts.GetNextIndex(iIndex))
    {
        CDailyTaskIdStatusData & stDailyTaskData = this->_hTaskCounts[iIndex];
        if( stDailyTaskData._iTaskClassType==_iTaskBelong )
        {
            iAllBelongCounts++;
            iTaskId = (unsigned int)stDailyTaskData._iTaskId;

            CMainCtrl::m_pstMain->m_stDailyTaskMgr.CheckTaskCeiling( *this, iTaskId, iDestNum, bHasCeiling );
            if( (unsigned int)stDailyTaskData._iCount>=iDestNum )
            {
                iFinishCounts++;
            }
        }
    }

    if( iAllBelongCounts!=0 && iFinishCounts==iAllBelongCounts )
    {
        if( this->_aClassTypeAwardeds[_iTaskBelong]==CONST_TBOX_CANOT_OPEN )
        {
            this->_aClassTypeAwardeds[_iTaskBelong] = CONST_TBOX_CAN_OPEN;
            m_bTaskTargetStatus = true;
        }
    }

    //检查状态时否更新
    this->UpdateTaskStatus(true); //推送到前端

    return iRet;
}

int CUserDailyTask::UpdateTaskStatus(bool bSendToFront)
{
    int iRet = 0;

    if( this->m_bTaskTargetStatus )
    {
        int iIndex = CMainCtrl::m_pstMain->m_stPlayerBaseInfoHash.HashFind(this->GetUserGid());
        if (iIndex < 0)
        {
            CT_ERROR(("user %u not in hash", this->GetUserGid()));
            return -1;
        }
        CPlayerBaseInfo& stPlayerBaseInfo = CMainCtrl::m_pstMain->m_stPlayerBaseInfoHash[iIndex];

        iRet = this->GetData( stPlayerBaseInfo._stUserDailyTaskData );
        CHECK_RETURN(iRet);

        //保存数据
        CVarObjectKey stKey;
        stKey.m_chDataType = DT_PLAYER_BASE_INFO;
        stKey.m_u64ID = stPlayerBaseInfo._uiGID;
        CMainCtrl::m_pstMain->UpdateData(stKey, stPlayerBaseInfo, VOP_UPDATE);

        if( bSendToFront )
        {
            //下发任务数据
            iRet = UpdateTaskList2Front();
            CHECK_RETURN(iRet);
        }
    }

    this->m_bTaskTargetStatus = false;

    return 0;
}

int CUserDailyTask::UpdateTaskList2Front()
{
    int iRet = 0;

    MsgAnsGetDailyTask stAns;

    int iIndex = CMainCtrl::m_pstMain->m_stPlayerOnlineDataHash.HashFind(_uiGID);
    if (iIndex < 0)
    {
        CT_ERROR(("get user online data info error, uid = %u", _uiGID));
        return RET_SYSTEM_FAIL;
    }
    CPlayerOnlineData& stPlayerOnlineData = CMainCtrl::m_pstMain->m_stPlayerOnlineDataHash[iIndex];

    CMainCtrl::m_pstMain->m_stDailyTaskMgr.GetTaskList(stPlayerOnlineData._stUserDailyTask, stAns);

    //取得用户每日统计数据 --start
    iIndex = CMainCtrl::m_pstMain->m_stPlayerSnsDetailInfoHash.HashFind(_uiGID);
    if (iIndex < 0)
    {
        CT_ERROR(("get user sns detail info error, uid = %u", _uiGID));
        return RET_SYSTEM_FAIL;
    }
    CPlayerSnsDetailInfo& stPlayerSnsDetailInfo = CMainCtrl::m_pstMain->m_stPlayerSnsDetailInfoHash[iIndex];
    CPlayerStatic& stPlayerStatic = stPlayerSnsDetailInfo._stMyInfo._stPlayerStatic;

    int iNowTime = CMainCtrl::m_pstMain->m_iNow;
    stAns.set_dailyexp(         stPlayerStatic._stTodayRoleGetExp.Get(iNowTime)                 );
    stAns.set_dailyreputation(  stPlayerStatic._stTodayGetReputation.Get(iNowTime)              );
    stAns.set_dailysilver(      stPlayerStatic._stTodayGetSilver.Get(iNowTime)                  );
    stAns.set_dailycontribution(stPlayerStatic._stTodayContribution.Get(iNowTime)               );
    //取得用户每日统计数据 --end

    PBMsgHead stMsgHead;
    stMsgHead.set_time(CMainCtrl::m_pstMain->m_iNow);
    stMsgHead.set_msgid(ID_MAIN_GET_DAILY_TASK);
    stMsgHead.set_msgret(RET_OK);
    stMsgHead.set_msgtype(CSMsgDef::EMT_ANSWER);

    CMainCtrl::m_pstMain->m_stEncodeBuf.Clear();
    CMainCtrl::m_pstMain->EncodePBMsg(stMsgHead, CMainCtrl::m_pstMain->m_stEncodeBuf);
    CMainCtrl::m_pstMain->EncodePBMsg(stAns, CMainCtrl::m_pstMain->m_stEncodeBuf);

    CMainCtrl::m_pstMain->NotifyOnlineUser(_uiGID, CMainCtrl::m_pstMain->m_stEncodeBuf);

    return iRet;
}


int CUserDailyTask::Init(const char* sBuff)
{
    int iRet = 0;

    iRet = _hTaskCounts.Init(sBuff, MAX_DAILY_TASK_CONFIG);
    CHECK_RETURN(iRet);

    return 0;
}

int CUserDailyTask::CheckDailyTaskUpdate()
{
    int iRet = 0;

    //最近是否更新过
    if( (CMainCtrl::m_pstMain->m_iNow-this->m_UpdateTime)<60 )
    {
        return iRet;
    }
    this->m_UpdateTime = CMainCtrl::m_pstMain->m_iNow;

    //检查小秘书到期
    if( this->_stSecretary._cStyle!=SECRETARY_TYPE_COMMON )
    {
        if( CMainCtrl::m_pstMain->m_iNow>this->_stSecretary._iLiveTime )
        {
            this->_stSecretary._cStyle = SECRETARY_TYPE_COMMON;
            this->_stSecretary._iLiveTime = 0;

            //秘书到期要推送消息到前端
            CTips stTips;
            stTips._iType = 25;
            CMainCtrl::m_pstMain->SendTips(_uiGID, stTips);

            m_bTaskTargetStatus = true;

            this->UpdateTaskStatus(false); //只保存，不推送到前端
        }
    }

    if( _stDailyLivess.IsSameDay(CMainCtrl::m_pstMain->m_iNow) )
    {
       return RET_OK; 
    }

    //重置每日数据
    iRet = this->ResetData();

    //查看有没有领取过当日活跃度奖励
    if( this->_stDailyLivess.Get(CMainCtrl::m_pstMain->m_iNow)>0 )
    {
        CSecretaryPayConfigData stData;
        iRet = CMainCtrl::m_pstMain->m_stDailyTaskMgr.GetSecretaryPayConfig(this->_stSecretary._cStyle, stData);
        CHECK_RETURN(iRet);

        if( stData._iDailyAddLiveness>0 )
        {
            this->_iLiveValuer += stData._iDailyAddLiveness;

            //根据活跃度更新宝箱的状态tbox 
            this->UpdateTBoxStatus(this->_iLiveValuer); 
        }

        this->_stDailyLivess.Sub(1, CMainCtrl::m_pstMain->m_iNow);

        m_bTaskTargetStatus = true;
    }
    else
    {
        //设置一下当天时间 
        if( this->_stDailyLivess.Get(CMainCtrl::m_pstMain->m_iNow)>0 )
        {
            _stDailyLivess.Sub(1, CMainCtrl::m_pstMain->m_iNow);
        }
    }


    m_bTaskTargetStatus = true;
    this->UpdateTaskStatus(); //保存，并推送到前端

    return iRet;
}

int CUserDailyTask::ResetData()
{
    int iRet = 0;

    this->_iLiveValuer = 0;
    this->_stDailyLivess.SetDefault(1);

    //tbox, 1:opened, 0:not open
    for( int i=0; i<this->_aOpenedTBox.Size(); ++i )
    {
       this->_aOpenedTBox[i%MAX_TBOX_NUM] = CONST_TBOX_CANOT_OPEN;
    }

    //task class award status,1:geted awarded, 0:not awarded 
    for( int i=0; i<this->_aClassTypeAwardeds.Size(); ++i )
    {
       this->_aClassTypeAwardeds[i%MAX_DAILY_TASK_CLASS_CONFIG] = CONST_TBOX_CANOT_OPEN;
    }

    for (int iIndex = this->_hTaskCounts.GetHeadIndex(); iIndex >= 0; iIndex = this->_hTaskCounts.GetNextIndex(iIndex))
    {
        if( this->_hTaskCounts[iIndex]._stYesterdayTaskFinish.IsYesterday(CMainCtrl::m_pstMain->m_iNow-86400) )
        {
            if( this->_hTaskCounts[iIndex]._iFindYesterdayOp>0 )
            {
                this->_hTaskCounts[iIndex]._stYesterdayTaskFinish.Add(this->_hTaskCounts[iIndex]._iFindYesterdayOp, CMainCtrl::m_pstMain->m_iNow-86400);
            }
            else
            {
                this->_hTaskCounts[iIndex]._stYesterdayTaskFinish.Add(this->_hTaskCounts[iIndex]._iCount, CMainCtrl::m_pstMain->m_iNow-86400);
            }
        }
        else
        {
            this->_hTaskCounts[iIndex]._stYesterdayTaskFinish.Add(0, CMainCtrl::m_pstMain->m_iNow-86400);
        }
        this->_hTaskCounts[iIndex]._iCount = 0;
        this->_hTaskCounts[iIndex]._iFindYesterdayOp = 0;
    }

    return iRet;
}

int CUserDailyTask::InitData(CDailyTaskUserData& stUserTaskData)
{
    int iRet = 0;
    Clean();

    this->_iLiveValuer = stUserTaskData._iLiveValuer;
    this->_stDailyLivess = stUserTaskData._stDailyLivess; 
    this->_stSecretary  = stUserTaskData._stSecretary; 

    //tbox, 1:opened, 0:not open
    this->_aOpenedTBox.m_iUsedCount = stUserTaskData._aOpenedTBox.Size();
    for( int i=0; i<stUserTaskData._aOpenedTBox.Size(); ++i )
    {
       this->_aOpenedTBox[i%MAX_TBOX_NUM] = stUserTaskData._aOpenedTBox[i];
    }

    //task class award status,1:geted awarded, 0:not awarded 
    this->_aClassTypeAwardeds.m_iUsedCount = MAX_DAILY_TASK_CLASS_CONFIG;
    for( int i=0; i<stUserTaskData._aTaskClassStatus.Size(); ++i )
    {
       this->_aClassTypeAwardeds[i%MAX_DAILY_TASK_CLASS_CONFIG] = stUserTaskData._aTaskClassStatus[i];
    }

    //任务类型从1开始，所以跳过0
    for( int i=0; i<stUserTaskData._aDailyTaskUserClass.Size(); ++i )
    {
        /*int iCursor = i%MAX_DAILY_TASK_CLASS_CONFIG;
        if( iCursor == stUserTaskData._aDailyTaskUserClass[i]._iTaskClassTypeId )
        {
            this->_aClassTypeAwardeds[iCursor] = stUserTaskData._aDailyTaskUserClass[i]._bAwarded;
        }*/

        CDailyTaskIdStatusData cdtidsd;
        CDTaskStatus cdts = stUserTaskData._aDailyTaskUserClass[i];

        cdtidsd._iTaskId = cdts.iTaskId;
        cdtidsd._iCount = cdts.iCount;
        cdtidsd._iTaskClassType = cdts._iTaskClassTypeId;
        cdtidsd._stYesterdayTaskFinish = cdts._stYesterdayTaskFinish;
        cdtidsd._iFindYesterdayOp = cdts._iFindYesterdayOp;
        
        int iIndex = this->_hTaskCounts.HashAlloc(cdtidsd._iTaskId);
        if( iIndex>=0 )
        {
            this->_hTaskCounts[iIndex] = cdtidsd;
        }
    }

    //新添加的每日任务老角色需要加入hash
    for (int i = 0;
         i < CMainCtrl::m_pstMain->m_stDailyTaskMgr.GetDailyTaskCnfSize(); ++i)
    {
        CDailyTaskConfigData& stTaskConfig =
            CMainCtrl::m_pstMain->m_stDailyTaskMgr.GetDailyTaskConfig(i);

        int iIndex = this->_hTaskCounts.HashFind(stTaskConfig._iTaskId);
        if (iIndex >= 0)
        {
            continue;
        }

        CDailyTaskIdStatusData cdtidsd;

        cdtidsd._iTaskId = stTaskConfig._iTaskId;
        cdtidsd._iCount = 0;
        cdtidsd._iTaskClassType = stTaskConfig._iTaskBelong;
        cdtidsd._stYesterdayTaskFinish.SetDefault(0);
        cdtidsd._iFindYesterdayOp = 0;
        
        iIndex = this->_hTaskCounts.HashAlloc(cdtidsd._iTaskId);
        if (iIndex >= 0)
        {
            this->_hTaskCounts[iIndex] = cdtidsd;
        }
        else
        {
            CT_ERROR(("role(%u) alloc daily task(%d) failed",
                _uiGID,cdtidsd._iTaskId));
        }
    }

    CheckDailyTaskUpdate();

    return iRet;
}

int CUserDailyTask::GetData(CDailyTaskUserData& stUserTaskData)
{
    int iRet = 0;
    stUserTaskData.Clear();

    stUserTaskData._iLiveValuer = this->_iLiveValuer;
    stUserTaskData._stDailyLivess = this->_stDailyLivess;
    stUserTaskData._stSecretary = this->_stSecretary;

    stUserTaskData._aOpenedTBox.m_iUsedCount = this->_aOpenedTBox.Size();
    for( int i=0; i<this->_aOpenedTBox.Size(); ++i )
    {
        stUserTaskData._aOpenedTBox[i%MAX_TBOX_NUM] = this->_aOpenedTBox[i];
    }

    for (int iIndex = this->_hTaskCounts.GetHeadIndex(); iIndex >= 0; iIndex = this->_hTaskCounts.GetNextIndex(iIndex))
    {
        //int iCursor = this->_hTaskCounts[iIndex]._iTaskClassType % MAX_DAILY_TASK_CLASS_CONFIG;
        //stUserTaskData._aDailyTaskUserClass[iCursor]._iTaskClassTypeId = this->_hTaskCounts[iIndex]._iTaskClassType;

        CDTaskStatus cdtStatusData;
        cdtStatusData.iTaskId = this->_hTaskCounts[iIndex]._iTaskId;
        cdtStatusData.iCount  = this->_hTaskCounts[iIndex]._iCount;
        cdtStatusData._iTaskClassTypeId = this->_hTaskCounts[iIndex]._iTaskClassType;
        cdtStatusData._stYesterdayTaskFinish = this->_hTaskCounts[iIndex]._stYesterdayTaskFinish;
        cdtStatusData._iFindYesterdayOp = this->_hTaskCounts[iIndex]._iFindYesterdayOp;

        //stUserTaskData._aDailyTaskUserClass[iCursor]._aDTClassStatus.AddOneItem( cdtStatusData );
        stUserTaskData._aDailyTaskUserClass.AddOneItem( cdtStatusData );
    }

    //任务类型从1开始，所以跳过0
    stUserTaskData._aTaskClassStatus.m_iUsedCount = _aClassTypeAwardeds.Size();
    for( int i=1; i<this->_aClassTypeAwardeds.Size(); ++i )
    {
        //stUserTaskData._aDailyTaskUserClass[i%MAX_DAILY_TASK_CLASS_CONFIG]._bAwarded = this->_aClassTypeAwardeds[i];
        stUserTaskData._aTaskClassStatus[i%MAX_DAILY_TASK_CLASS_CONFIG] = this->_aClassTypeAwardeds[i];
        /*if( i%MAX_DAILY_TASK_CLASS_CONFIG == this->_aClassTypeAwardeds[i]._iTaskClassTypeId )
        {
            stUserTaskData._aDailyTaskUserClass[i%MAX_DAILY_TASK_CLASS_CONFIG]._bAwarded = this->_aClassTypeAwardeds[i];
        }
        else
        {
            stUserTaskData._aDailyTaskUserClass[i%MAX_DAILY_TASK_CLASS_CONFIG]._bAwarded = false;
        }*/
    }

    stUserTaskData._stDailyLivess = this->_stDailyLivess;
    stUserTaskData._stSecretary = this->_stSecretary;

    return iRet;
}

/*
bool CUserDailyTask::IsTaskFinished(CTaskID& stTaskID)
{
    return _stTaskStatus.IsFinished(stTaskID);
}
*/

void CUserDailyTask::SetUserGid(unsigned int uiGid)
{
		this->_uiGID = uiGid;
}

unsigned int CUserDailyTask::GetUserGid() const
{
	return this->_uiGID;
}

int CUserDailyTask::GMSetDailyTask(int iLiveness, int iTBoxId)
{
    int iRet = 0;

    this->_iLiveValuer = iLiveness;

    //根据活跃度更新宝箱的状态tbox 
    this->UpdateTBoxStatus(this->_iLiveValuer); 

    m_bTaskTargetStatus = true;

    this->UpdateTaskStatus();

    return iRet;
}

int CUserDailyTask::GetRandomRange(int iMin, int iMax)
{
    int iResult = 0;

    while(true)
    {
        iResult = random()%(iMax-iMin) + iMin;
        if( iResult>=iMin && iResult<=iMax ) break;
    }

    return iResult;
}

int CUserDailyTask::DiyLuckDoubleAwardBox(int& iFucklyBoxId)
{
    int iRet = 0;
    bool bHasOped = false;
    int iBoxBase = 2; //双倍奖励的从第3个宝箱开始

    //检查是否已经获取过试试手运操作
    for( int i=iBoxBase; i<this->_aOpenedTBox.Size(); ++i )
    {
        if( this->_aOpenedTBox[i]>=CONST_DOUBLE_AWARD_BASE )
        {
            bHasOped = true;
            break;
        }
    }
    if( bHasOped )
    {
        return ERROR_HAS_CLICK_DIY_DOUBLE_AWARD_BOX;
    }

    //是否所有宝箱都已经开启
    for( int i=iBoxBase; i<this->_aOpenedTBox.Size(); ++i )
    {
        if( this->_aOpenedTBox[i]!=CONST_TBOX_OPENED )
        {
            bHasOped = true;
            break;
        }
    }
    if( !bHasOped )
    {
        return ERROR_ALL_TBOX_IS_OPENED;
    }

    iFucklyBoxId = GetRandomRange(iBoxBase, this->_aOpenedTBox.Size());
    int iBoxIdPos = (iFucklyBoxId)%this->_aOpenedTBox.Size();
    if( this->_aOpenedTBox[iBoxIdPos]==CONST_TBOX_OPENED )
    {
        while(true)
        {
            if( this->_aOpenedTBox[iBoxIdPos]==CONST_TBOX_OPENED )
            {
                iBoxIdPos = (iBoxIdPos+1)%this->_aOpenedTBox.Size();
                if( iBoxIdPos==0 ) iBoxIdPos = iBoxBase;
                continue;
            }

            iFucklyBoxId = iBoxIdPos + 1;
            break;
        }
    }
    iFucklyBoxId = iBoxIdPos + 1;
    this->_aOpenedTBox[iBoxIdPos] = CONST_DOUBLE_AWARD_BASE + this->_aOpenedTBox[iBoxIdPos];

    m_bTaskTargetStatus = true;

    this->UpdateTaskStatus();

    return iRet;
}

int CUserDailyTask::BuySecretaryStyle( int iSecretaryStyle, int iNeedGold )
{
    int iRet = 0;
    int iIndex = CMainCtrl::m_pstMain->m_stPlayerBaseInfoHash.HashFind( this->_uiGID );
    CPlayerBaseInfo& stPlayerBaseInfo = CMainCtrl::m_pstMain->m_stPlayerBaseInfoHash[iIndex];
    
    //检查是否已经是此形象小秘书
    if( this->_stSecretary._cStyle == iSecretaryStyle )
    {
        return ERROR_SECRETARY_BUY_SAME_STYLE;
    }

    this->_stSecretary._cStyle = iSecretaryStyle;
    this->_stSecretary._iLiveTime = CMainCtrl::m_pstMain->m_iNow + SECRETARY_LIVE_TIME;
    iRet = CMainCtrl::m_pstMain->m_stItemMgr.SubGold(stPlayerBaseInfo, OSS_MONEY_MODULE_30, iNeedGold);

    //推送金币信息
    CMainCtrl::m_pstMain->m_stNotifyMgr.Notify(stPlayerBaseInfo._uiGID, NOTIFY_USER_MONEY);

    m_bTaskTargetStatus = true;

    this->UpdateTaskStatus();

    return iRet;
}

//类型奖励
int CUserDailyTask::GetTaskClassAwardItems(CDailyTaskClassAwardConfigData& curTaskCfgData, CDropItem& stDropItem)
{
	int iRet = 0;
	CDropItemData stDropItemData;

    unsigned char ucType = 0;
    

    if( ucType==ITEM_TYPE_EXP ) //等级奖励基数
    {
        ucType = ITEM_TYPE_LEVEL_EXP;
    }
    else if( ucType ==ITEM_TYPE_SILVER )
    {
        ucType = ITEM_TYPE_LEVEL_SILVER;        
    }
    else
    {
        ucType = curTaskCfgData._iAwardItemType;
    }

    int iCount = curTaskCfgData._iAwardNumCommon;
    iRet = CMainCtrl::m_pstMain->m_stItemMgr.GetDropItemData(GetUserGid(), ucType, curTaskCfgData._iAwardItemId, NULL,
                                                             COMM_MOD, iCount, stDropItemData);
    if (iRet)
    {
        CT_ERROR(("%d, %d, %d, %d", iRet, ucType, iCount, GetUserGid()));
        return iRet;
    }

    stDropItem.Drop(stDropItemData);
	return RET_OK;
}

//宝箱奖励需要考虑小秘书奖励加成
int CUserDailyTask::GetTaskAwardItems(CDailyTaskTboxAwardConfigData& curTaskCfgData, CSecretaryLevelConfigData& curLevelCfgData, CDropItem& stDropItem, bool bDoubleAward)
{
	int iRet = 0;
	CDropItemData stDropItemData;
    int iEndAwardNum = 0;

	for (int i = 0; i < curTaskCfgData._aTboxAwards.Size(); ++i)
	{
		const CDailyTaskTboxAwardConfigData::TBoxAward& OneTaskAward = curTaskCfgData._aTboxAwards[i];

        iEndAwardNum = OneTaskAward.iAwardNum;
        if( this->_stSecretary._cStyle>0 )
        {
            if( OneTaskAward.iAwardItemType==ITEM_TYPE_REPUTATION )
            {
                if( this->_stSecretary._cStyle==SECRETARY_TYPE_PRESTIGE ) //声望秘书
                {
                    iEndAwardNum = ((100+curLevelCfgData._iPrestigeAdd)*iEndAwardNum)/100;
                }
            }
            else if( OneTaskAward.iAwardItemType==ITEM_TYPE_SILVER )
            {
                if( this->_stSecretary._cStyle==SECRETARY_TYPE_SILVER ) //银币秘书
                {
                    iEndAwardNum = ((100+curLevelCfgData._iSilverAdd)*iEndAwardNum)/100;
                }
            }
        }

        //双倍奖励
        if( bDoubleAward )
        {
            iEndAwardNum = iEndAwardNum*2;
        }

        CMainCtrl::m_pstMain->m_stItemMgr.GetDropItemData(GetUserGid(), OneTaskAward.iAwardItemType, 
						OneTaskAward.iAwardItemId, NULL, COMM_MOD, iEndAwardNum, stDropItemData);

		iRet = stDropItem.Drop(stDropItemData);
	}

	return iRet;
}

/*
int CUserDailyTask::GMSetTaskFinished(CTaskID & stTaskID)
{
    int iRet = 0;

    iRet = this->_stTaskStatus.SetTaskFinished( stTaskID );

    // Delete condition tasks in accept task array
    for( int i=0; i<_astOpenTaskIDs.Size(); ++i )
    {
        while( stTaskID.IsMainTask() == _astOpenTaskIDs[i].IsMainTask() )
        {
            if( stTaskID._uiBranchID >= _astOpenTaskIDs[i]._uiBranchID 
                && stTaskID._uiTaskID >= _astOpenTaskIDs[i]._uiTaskID )
            {
                _astOpenTaskIDs.DelOneItem(i);
                if( _astOpenTaskIDs.Size() > i )
                {
                    continue;
                }
                else
                {
                    break;
                }
            }
        }
    }

    for( int i=0; i<_astValidTasks.Size(); ++i )
    {
        while( stTaskID.IsMainTask() == _astValidTasks[i].GetTaskID().IsMainTask() )
        {
            if( stTaskID._uiBranchID >= _astValidTasks[i].GetTaskID()._uiBranchID 
                && stTaskID._uiTaskID >= _astValidTasks[i].GetTaskID()._uiTaskID )
            {
                _astValidTasks.DelOneItem(i);
                if( _astValidTasks.Size() > i )
                {
                    continue;
                }
                else
                {
                    break;
                }
            }
        }
    }

    return iRet;
}
*/

